package com.sh.mytool.cv.domain;

import java.io.Serializable;
import javax.persistence.*;
import static javax.persistence.CascadeType.REFRESH;
import static javax.persistence.CascadeType.PERSIST;
import static javax.persistence.FetchType.EAGER;
import java.util.List;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.CascadeType.REMOVE;
import com.sh.mytool.cv.domain.Article;

@SuppressWarnings("serial")
@Entity
@Table(name = "tb_column")
public class Column implements Serializable {

    @Id
    @GeneratedValue
    @javax.persistence.Column(name = "column_id", length = 20, nullable = false)
    private Long id;

    @javax.persistence.Column(name = "column_text", length = 200, nullable = false)
    private String text;

    // 自己是子
    @ManyToOne(cascade = REFRESH, optional = true, fetch = EAGER)
    @JoinColumn(name = "column_parent_id", referencedColumnName = "column_id")
    private Column parent;

    // 自己是父，依赖于另一方parent
    @OneToMany(fetch = LAZY, cascade = { REMOVE, REFRESH }, mappedBy = "parent")
    @OrderBy(value = "orderno ASC")
    private List<Column> children;

    @Transient 
    private Boolean leaf;
    
    @javax.persistence.Column(name = "column_orderno", length = 20, nullable = false)
    private Integer orderno;

    @OneToOne(cascade = { REFRESH, PERSIST, REMOVE }, fetch = LAZY, mappedBy = "column")
    private Article article;

    public Boolean getLeaf() {
        return (children.size() == 0);
    }

    public void setLeaf(Boolean leaf) {
        this.leaf = leaf;
    }

    public void addChildren(Column param){
        param.setParent(this);
        children.add(param);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Column getParent() {
        return parent;
    }

    public void setParent(Column parent) {
        this.parent = parent;
    }

    public List<Column> getChildren() {
        return children;
    }

    public void setChildren(List<Column> children) {
        this.children = children;
    }

    public Integer getOrderno() {
    	return orderno;
    }

	public void setOrderno(Integer orderno) {
    	this.orderno = orderno;
    }

	public String getText() {
        return text;
    }

    public void setText(String name) {
        this.text = name;
    }

    public Article getArticle() {
        return article;
    }

    public void setArticle(Article param) {
        this.article = param;
    }

}